
import os
import project
import peopletomethods

def createTables(db):
    q = """
CREATE TABLE changes(
	ChangeID	serial	NOT NULL,
	OldFile 	int	references Files(FileID) NOT NULL,
	NewFile 	int	references Files(FileID) NOT NULL,
PRIMARY KEY (ChangeID)
);

CREATE TABLE changedetails(
	DetailID	serial		NOT NULL,
	ChangeID	int		references Changes(ChangeID) NOT NULL,
	FuncID		int		references Functions(FuncID) NOT NULL,
	lineschanged	int 		NOT NULL,
	changetype	char(1)		NOT NULL,
PRIMARY KEY  (DetailID)
);
"""
    dropq = """
drop table changes cascade;
drop table changedetails cascade;
"""

    try:
        print "cleaning Changes tables..."
        db.query(dropq)
        print "...OK!"
    except _pg.ProgrammingError:
            print "could not drop tables"
    try:
        print "creating Changes tables..."
        db.query(q)
        print "...OK!"
    except _pg.ProgrammingError, e:
        print "could not create tables"
        raise project.ProjectError("Could not create tables, cause:" + str(e))



def start(project, log):
    db = proj.getDatabase()
    repo = proj.getSVNRepository()
    peopletomethods.mapPeopleToMethods(db, repo, log)

    
